package com.kangyi.user.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.FavoriteDTO;
import com.kangyi.entity.NursingHome;
import com.kangyi.entity.NursingHomeFavorite;
import com.kangyi.exception.AddException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.user.mapper.*;
import com.kangyi.user.service.NursingHomeFavoriteService;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class NursingHomeFavoriteServiceImpl implements NursingHomeFavoriteService {

    @Autowired
    private NursingHomeFavoriteMapper nursingHomeFavoriteMapper;

    @Autowired
    private NursingHomeMapper nursingHomeMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private NursingHomeTagMapper nursingHomeTagMapper;



    @Override
    public List<NursingHomeFavoriteVO> listFavorite(Integer userId) {
        // 查询用户收藏的养老院
        List<NursingHomeFavoriteVO> nursingHomeFavoriteVOList = nursingHomeFavoriteMapper.listFavorite(userId);

        if (CollUtil.isEmpty(nursingHomeFavoriteVOList)) {
            throw new DataNotExistException("该用户没有收藏任何养老院");
        }

        // 分别查询每个养老院的详细信息
        nursingHomeFavoriteVOList.forEach(favorite -> {
            Integer nursingHomeId = favorite.getNursingHomeListFavoriteVO().getId();

            // 查询养老院的详细信息
            NursingHomeListFavoriteVO nursingHome = nursingHomeMapper.getNursingHomeDetailsById(nursingHomeId);

            // 查询养老院的地区信息
            RegionVO region = regionMapper.getRegionById(nursingHomeId);
            nursingHome.setRegionVO(region);

            // 查询养老院的标签
            List<NursingHomeTagVO> tags = nursingHomeTagMapper.getTagsByNursingHomeId(nursingHomeId);
            nursingHome.setNursingHomeTagVOList(tags);

            // 设置到收藏数据VO中
            favorite.setNursingHomeListFavoriteVO(nursingHome);
        });

        return nursingHomeFavoriteVOList;
    }

    @Override
    public void addFavorite(FavoriteDTO favoriteDTO) {
        NursingHome nursingHome = nursingHomeMapper.selectById(favoriteDTO.getNursingHomeId());
        if (nursingHome == null) {
            throw new DataNotExistException(MessageConstant.NURSING_HOME_NOT_EXIST);
        }
        NursingHomeFavorite Favorite =nursingHomeFavoriteMapper.selectByUserIdAndNursingHomeId(favoriteDTO);
        if (Favorite != null) {
            throw new AddException(MessageConstant.COLLECTED_NURSING_HOME);
        }

        NursingHomeFavorite nursingHomeFavorite = new NursingHomeFavorite();
        BeanUtils.copyProperties(favoriteDTO,nursingHomeFavorite);
        nursingHomeFavoriteMapper.addFavorite(nursingHomeFavorite);
    }

    @Override
    public void cancelFavorite(FavoriteDTO favoriteDTO) {
        Integer userId = favoriteDTO.getUserId();
        AuthUtil.validateUserPermission(userId);
        nursingHomeFavoriteMapper.cancelFavorite(favoriteDTO);
    }

    @Override
    public void deleteFavorite(Integer id) {
        NursingHomeFavorite nursingHomeFavorite = nursingHomeFavoriteMapper.selectById(id);
        if (nursingHomeFavorite == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        Integer userId = nursingHomeFavorite.getUserId();
        AuthUtil.validateUserPermission(userId);

        nursingHomeFavoriteMapper.deleteById(id);
    }

}
